package com.fieldIndustry.struts2.action;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.fieldIndustry.pojo.Authorcode;
import com.fieldIndustry.pojo.Downloadcate;
import com.fieldIndustry.pojo.Fieldindustry;
import com.fieldIndustry.pojo.Industryfbd;
import com.fieldIndustry.service.IAuthorcodeService;
import com.fieldIndustry.service.IDownloadcateService;
import com.fieldIndustry.service.IFieldindustryService;
import com.fieldIndustry.service.IIndustryfbdService;
import com.opensymphony.xwork2.ActionSupport;
import com.uwntek.pojo.Projectmanage;
import com.uwntek.service.IProjectManageService;

public class DownLoadAction extends ActionSupport {
	// private String authorCodeStr = "01_000101_000101_20120630_asdf";
	private String categoryStr;
	private String industryStr;
	private String industryFbdStr;
	private String date;
	private String filePath;
	private String fileName;

	private Downloadcate category;
	private Fieldindustry industry;
	private Industryfbd industryfbd;
	private Authorcode authorcode;
	private IDownloadcateService downloadcateSer;
	private IFieldindustryService fieldindustrySer;
	private IIndustryfbdService industryfbdSer;
	private IAuthorcodeService authorcodeSer;

	private Projectmanage project;
	private IProjectManageService<Projectmanage> projectManageSer;

	HttpServletRequest request;
	HttpServletResponse response;
	
	public String execute() throws Exception {
		if (!authorcode())
			return "fail";
		
		return SUCCESS;
	}
	public String getPath()  {
		/**
		 * authorCodeStr = "01_000101_000101_20120630_asdf"
		 * 01表示行业库 000101表示行业及版本 000101表示行业算法及版本
		 * 02表示图库 
		 */

		// 获取行业库、图库下载路径
		if(project==null){
		if (categoryStr != null && industryStr != null
				&& industryFbdStr != null) {
			//获取下载类型：行业库or图库
			category = (Downloadcate) downloadcateSer.find(Downloadcate.class,
					Integer.valueOf(categoryStr));
			if(category.getCategory().equals("行业库"))
			{
				if(industryStr.equals("000000") && industryFbdStr.equals("000000"))
				{ 
					//下载整个行业库包
					filePath = category.getCatePath();
				}else if(!industryStr.equals("000000")
						&& industryFbdStr.equals("000000")){
					//下载指定行业包
					industryStr = industryStr.substring(0, 4);
					industry = (Fieldindustry) fieldindustrySer.find(
							Fieldindustry.class, Integer.valueOf(industryStr));
					filePath = industry.getFdIndustryPath();
				}else{
					//下载指定行业库指定行业算法包
					industryFbdStr = industryFbdStr.substring(0, 4);
					industryfbd = (Industryfbd) industryfbdSer.find(
							Industryfbd.class, Integer.valueOf(industryFbdStr));
					filePath = industryfbd.getIndustryFbdPath();
				}
			}else if(category.getCategory().equals("图库"))
			{
				if(industryStr.equals("000000") && industryFbdStr.equals("000000"))
				{ 
					//下载整个图库包
					filePath = category.getCatePath();
				}else if(!industryStr.equals("000000")
						&& industryFbdStr.equals("000000")){
					//下载指定类型的图库包
					
					
				}else{
					//下载指定类型图库的指定图库文件包


				}
			}
		}
//			if (industryStr.equals("000000") && industryFbdStr.equals("000000")) {
//				filePath = category.getCatePath();
//			} else if (!industryStr.equals("000000")
//					&& industryFbdStr.equals("000000")) {
//				if (category.getCategory().equals("行业库")) {
//					industryStr = industryStr.substring(0, 4);
//					industry = (Fieldindustry) fieldindustrySer.find(
//							Fieldindustry.class, Integer.valueOf(industryStr));
//					filePath = industry.getFdIndustryPath();
//				}
//			} else {
//				if (category.getCategory().equals("行业库")) {
//					industryFbdStr = industryFbdStr.substring(0, 4);
//					industryfbd = (Industryfbd) industryfbdSer.find(
//							Industryfbd.class, Integer.valueOf(industryFbdStr));
//					filePath = industryfbd.getIndustryFbdPath();
//				}
//			}
//		}
		}else{
		   //工程包下载
			project = projectManageSer.find(Projectmanage.class , project.getId());
			filePath =project.getProjectPath();
		}
		
		return filePath;
	}

	// 从下载文件原始存放路径读取得到文件输出流
	public InputStream getDownloadFile() throws IOException {
		filePath = getPath();
		// ServletActionContext.getServletContext().getResourceAsStream(filePath);
		InputStream in = new FileInputStream(filePath);
		if(null==in){
			System.out.println("Can not find a java.io.InputStream with the name [inputStream] in the invocation stack. Check the <param name=\"inputName\"> tag specified for this action.检查action中文件下载路径是否正确."); 
			}

		return in;
	}

	// 如果下载文件名为中文，进行字符编码转换
	public String getDownloadChineseFileName() {
		response = ServletActionContext.getResponse();
		request = ServletActionContext.getRequest();
		String temp[] = filePath.replaceAll("\\\\","/").split("/");
		if(temp.length > 1){
		    fileName = temp[temp.length - 1];
		}
		String downloadChineseFileName = fileName;
		String agent = (String) request.getHeader("USER-AGENT");
		try {
		if (agent != null && agent.indexOf("MSIE") == -1) {
			// FF
			
				downloadChineseFileName = new String(fileName.getBytes("UTF-8"));
			
		} else {
			// IE
			downloadChineseFileName = new String(fileName.getBytes("GBK"),
					"ISO-8859-1");
			response.setHeader("Content-Disposition", "attachment; filename="
					+ downloadChineseFileName);
		}
		
		downloadChineseFileName = new String(
				downloadChineseFileName.getBytes(), "ISO8859-1");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			System.out.println("中文转义出错");
			e.printStackTrace();
		
		}
		return downloadChineseFileName;
	}


	public boolean authorcode() throws IOException {
		if(authorcode!=null&&project==null){
		if (authorcode.getAuthorCode().length() == 30) {
			if (!authorcodeSer.createAuthor(authorcode))
			 {
				 addActionMessage("授权码码已使用，重新购买！");
				 return false;
			  } else {
				  //判断授权码是否过期
					String[] strs = authorcode.getAuthorCode().split("_");
					this.categoryStr = strs[0];
					this.industryStr = strs[1];
					this.industryFbdStr = strs[2];
					this.date = strs[3];
					SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");// 设置日期格式
					System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
					String nowDate = df.format(new Date());
					if (nowDate.compareTo(date) > 0) {
						addActionMessage("授权码已过期，重新购买！");
						return false;
					} else {
						return true;
					}
			  	 }
			} else {
				addActionMessage("授权码输入有误，请确认！");
				return false;
			}}
		else{
			return true;
		}

	}


	public Authorcode getAuthorcode() {
		return authorcode;
	}

	public void setAuthorcode(Authorcode authorcode) {
		this.authorcode = authorcode;
	}

	public IAuthorcodeService getAuthorcodeSer() {
		return authorcodeSer;
	}

	public void setAuthorcodeSer(IAuthorcodeService authorcodeSer) {
		this.authorcodeSer = authorcodeSer;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName)
			throws UnsupportedEncodingException {
		this.fileName = new String(fileName.getBytes("ISO8859-1"), "utf-8");

	}

	public String getCategoryStr() {
		return categoryStr;
	}

	public void setCategoryStr(String categoryStr) {
		this.categoryStr = categoryStr;
	}

	public String getIndustryStr() {
		return industryStr;
	}

	public void setIndustryStr(String industryStr) {
		this.industryStr = industryStr;
	}

	public String getIndustryFbdStr() {
		return industryFbdStr;
	}

	public void setIndustryFbdStr(String industryFbdStr) {
		this.industryFbdStr = industryFbdStr;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public Downloadcate getCategory() {
		return category;
	}

	public void setCategory(Downloadcate category) {
		this.category = category;
	}

	public Fieldindustry getIndustry() {
		return industry;
	}

	public void setIndustry(Fieldindustry industry) {
		this.industry = industry;
	}

	public Industryfbd getIndustryfbd() {
		return industryfbd;
	}

	public void setIndustryfbd(Industryfbd industryfbd) {
		this.industryfbd = industryfbd;
	}

	public IDownloadcateService getDownloadcateSer() {
		return downloadcateSer;
	}

	public void setDownloadcateSer(IDownloadcateService downloadcateSer) {
		this.downloadcateSer = downloadcateSer;
	}

	public IFieldindustryService getFieldindustrySer() {
		return fieldindustrySer;
	}

	public void setFieldindustrySer(IFieldindustryService fieldindustrySer) {
		this.fieldindustrySer = fieldindustrySer;
	}

	public IIndustryfbdService getIndustryfbdSer() {
		return industryfbdSer;
	}

	public void setIndustryfbdSer(IIndustryfbdService industryfbdSer) {
		this.industryfbdSer = industryfbdSer;
	}

	public Projectmanage getProject() {
		return project;
	}

	public void setProject(Projectmanage project) {
		this.project = project;
	}

	public IProjectManageService<Projectmanage> getProjectManageSer() {
		return projectManageSer;
	}

	public void setProjectManageSer(
			IProjectManageService<Projectmanage> projectManageSer) {
		this.projectManageSer = projectManageSer;
	}

}
